<template>
  <div class="login">
    <el-form
      ref="loginFormRef"
      :rules="loginRules"
      class="myform"
      :model="loginForm"
    >
      <h3>iHRM 后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input
          v-model="loginForm.mobile"
          prefix-icon="el-icon-user-solid"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          prefix-icon="el-icon-lock"
          show-password
        />
      </el-form-item>
      <el-form-item>
        <el-button
          class="btn"
          @click="login"
        >登录</el-button>
      </el-form-item>
      <el-form-item>
        <div class="txt">还没有帐号？立即注册</div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        mobile: '13800000002',
        password: '123456'
      },
      loginRules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            validator: (ruler, value, cb) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value) ? cb() : cb(new Error('手机号不合法'))
            }
          }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 3, max: 6, message: '密码长度在 3 到 6 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions('user', ['vuexLogin']),
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return this.$message.error('登录失败')
        try {
          await this.vuexLogin(this.loginForm)
          const _redirect = this.$route.query._redirect
          if (_redirect) {
            this.$message.success('重连成功')
            this.$router.push(_redirect)
          } else {
            this.$message.success('登录成功')
            this.$router.push('/')
          }
        } catch (error) {
          this.$message.error(error.message)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  background-image: url("../../assets/common/login.jpg");
  background-position: center;
  height: 100%;
  .myform {
    width: 520px;
    height: 400px;
    padding: 35px;
    box-sizing: border-box;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -76%);
    h3 {
      text-align: center;
      font-size: 48px;
      color: #fff;
      font-weight: 100;
      margin: 40px 0;
      letter-spacing: 2px; // 设置文本间隔
    }
    ::v-deep input.el-input__inner {
      height: 50px;
      line-height: 50px;
      background-color: #d4e5ff;
      padding-left: 40px;
      color: #68b0fe;
    }
    ::v-deep span.el-input__prefix {
      margin-left: 5px;
    }
    .btn {
      background-color: #407ffe;
      color: #fff;
      width: 100%;
      height: 64px;
      border-color: #999;
      font-size: 24px;
    }
    .txt {
      text-align: center;
      color: #fff;
      font-size: 16px;
    }
    ::v-deep .el-form-item__error {
      color: #fff;
    }
  }
}
</style>
